const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const utils = require('../../utils.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classInfoDTOs:[],
    currentClass:{},
    groupDTOs:[],
    allCLassState:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getobtainClassInfos()
  },

  // 全局捕获
  bindViewTap(event) {
    if (event.target.id != 'classId') {
      this.selectComponent("#classId").hideFn()
    }
  },
  
  // 班级切换操作
  classSwitch(e) {
    this.setData({
      allCLassState: !this.data.allCLassState
    })
  },

  // 切换班级
  switchClass(value) {
    let curData = value.detail.currentClass

    var classlist = {
      name: curData.name,
      ciId: curData.ciId,
      groups: curData.groups,
      isMember: curData.isMember
    }

    this.setData({
      currentClass: classlist
    })

    this.obtainGroups()
  },

  // 获取班级信息
  getobtainClassInfos() {
    utils.ajaxFn({
      url: config.service.obtainClassInfos,
      callback: (data) => {
       
        this.setData({
          classInfoDTOs:data.classInfoDTOs.filter(value => !!value.isAdmin)
        })

        var classlist = {
          name: this.data.classInfoDTOs[0].name,
          ciId: this.data.classInfoDTOs[0].ciId,
          groups: this.data.classInfoDTOs[0].groups,
          isMember: this.data.classInfoDTOs[0].isMember
        }

        this.setData({
          currentClass: classlist
        })

        this.obtainGroups()
      }
    })
  },

  // 获取群组列表
  obtainGroups() {
    utils.ajaxFn({
      url: config.service.obtainGroups,
      body:{
        ciId: this.data.currentClass['ciId']
      },
      callback: (data) => {
        this.setData({
          groupDTOs: data.groupDTOs
        })
      }
    })
  },

  router() {
    wx.navigateTo({
      url: `/pages/addGroup/addGroup?ciId=${this.data.currentClass['ciId']}`
    })
  },

  editRouter(e) {
    wx.navigateTo({
      url: `/pages/editGroup/editGroup?cgid=${e.currentTarget.dataset.cgid}&ciId=${this.data.currentClass['ciId']}&name=${e.currentTarget.dataset.name}`
    })
  }
})